﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using Model.Models;
using System.Linq;

namespace LogisticsDAL
{
    public class customerSql : Icustomer
    {
        private readonly LogisticsDBContext context;

        public customerSql(LogisticsDBContext context)
        {
            this.context = context;
        }

        public string AddCustomer(Customer cu)
        {
            context.Customers.Add(cu);
            int sum = context.SaveChanges();
            if (sum ==0)
            {
                return "添加失败";
            }
            return "添加成功";
        }

        public List<Customer> getAll()
        {
            return context.Customers.ToList();
        }

        public List<Customer> GetInfo(string card, string cname, string mobile)
        {
            return context.Customers.Where(a => a.Card.Contains(card) && a.Cname.Contains(cname) && a.Mobile.Contains(mobile)).ToList();
        }

        public string update(Customer cs)
        {
            //把传进来的数据固定在上下文对象中的实体上
            var customer = context.Customers.Attach(cs);
            //标记该实体对象允许被修改
            customer.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            context.SaveChanges();
            return "修改成功";
        }
    }
}
